package com.zkyc.framework.website.controller;

import com.zkyc.framework.common.Enum.LogType;
import com.zkyc.framework.common.annotation.Log;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.pojo.Association;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.website.service.AssociationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Association)表控制层
 *
 * @author wj
 * @since 2022-11-15 19:06:03
 */
@RestController
@RequestMapping("association")
public class AssociationController {
    /**
     * 服务对象
     */
    @Resource(name = "associationService")
    private AssociationService associationService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("list")
    @HystrixCommand(fallbackMethod = "queryByPageFallback")
    public ResultVO queryByPage() {
        return associationService.queryByPage();
    }
    public ResultVO queryByPageFallback() {
        return ResultVO.success("访问超时,请稍后再试");
    }

    /**
     * 分页查询剪影
     *
     * @return 查询结果
     */
    @GetMapping("silhouette")
    @HystrixCommand(fallbackMethod = "querySilhouetteByPageFallback")
    public ResultVO querySilhouetteByPage(Integer associationId, Integer fileType,
                                          @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                          @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return associationService.querySilhouetteByPage(associationId, fileType, current, size);
    }
    public ResultVO querySilhouetteByPageFallback(Integer associationId, Integer fileType,
                                          @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                          @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return ResultVO.success("访问超时,请稍后再试");
    }

    /**
     * 根据id查询剪影
     *
     * @return 查询结果
     */
    @GetMapping("silhouetteById")
    @HystrixCommand(fallbackMethod = "querySilhouetteByIdFallback")
    public ResultVO querySilhouetteById(Long id) {
        return associationService.querySilhouetteById(id);
    }
    public ResultVO querySilhouetteByIdFallback(Long id) {
        return ResultVO.success("访问超时,请稍后再试");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public ResultVO queryById(@RequestParam("id") Integer id) {
        Association association = associationService.queryById(id);
        if (association == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        return ResultVO.data(association);
    }

    /**
     * 新增数据
     *
     * @param association 实体
     * @return 新增结果
     */
    @Log(type = LogType.新增协会)
    @PostMapping("save")
    public ResultVO add(@RequestBody Association association) {
        return associationService.insert(association);
    }

    /**
     * 编辑数据
     *
     * @param association 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    @Log(type = LogType.编辑协会)
    public ResultVO edit(@RequestBody Association association) {
        return associationService.update(association);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("delete")
    public ResultVO deleteById(Integer id) {
        return associationService.deleteById(id);
    }

}

